package com.filldream.fastboot.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @author RickSun
 * @description 类型转换
 **/
@Slf4j
public class TransferUtil {

    public static <T> List<T> changeToList(List beforeList, Class cla){
        List<T> resultList = new ArrayList<>();
        try {
            for (Object obj : beforeList) {
//                T t = (T)cla.newInstance();
                T t = (T)cla.getDeclaredConstructor().newInstance();
                BeanUtils.copyProperties(obj, t);
                resultList.add(t);
            }
        }catch (Exception e){
            log.warn("TransferUtil的changeToList对象转换异常:{}",e.getMessage());
        }
        return resultList;
    }

    public static <T>T changeToObj(Object obj,Class cla){
        if(obj == null){
            return null;
        }
        try {
            T t = (T)cla.getDeclaredConstructor().newInstance();
//            T t = (T) cla.newInstance();
            BeanUtils.copyProperties(obj,t);
            return t;
        }catch (Exception e){
            log.warn("TransferUtil的changeToObj对象转换异常:{}",e.getMessage());
            return null;
        }
    }


}
